/* eslint-disable */
// import { QWebChannel } from './qwebchannel.js'

const REQUEST_URL = 'http://192.168.1.62:61100/aiplatform'

// 判断Qt环境 处理浏览器报错
export const isQtClient = (function () {
    return navigator.userAgent.includes('QtWebEngine')
})()

if (!isQtClient) {
    window.qt = {
        webChannelTransport: {
            send() {
                console.log(`QWebChannel simulator activated !`)
            }
        }
    }
}

// utils
const getQueryParams = function (params, isStringParam) {
    if (isStringParam) {
        const url = new URLSearchParams(params)
        let result = {}
        for (const pair of url.entries()) {
            result[pair[0]] = pair[1]
        }
        return result
    } else {
        return params
    }
}
export const generateRandomUUID = function () {
    const hexList = []
    for (let i = 0; i <= 15; i++) {
        hexList[i] = i.toString(16)
    }
    let uuid = ''
    for (let i = 1; i <= 36; i++) {
        if (i === 9 || i === 14 || i === 19 || i === 24) {
            uuid += '-'
        } else if (i === 15) {
            uuid += 4
        } else if (i === 20) {
            uuid += hexList[(Math.random() * 4) | 8]
        } else {
            uuid += hexList[(Math.random() * 16) | 0]
        }
    }
    // return uuid.replace(/-/g, "");
    return uuid
}
export const convertIdsToQueryString = function (ids) {
    let result = ''
    for (let i = 0; i < ids.length; i++) {
        result += 'ids=' + ids[i]
        if (i < ids.length - 1) {
            result += '&'
        }
    }
    return '?' + result
}
export const convertIdListToQueryString = function (ids) {
    let result = ''
    for (let i = 0; i < ids.length; i++) {
        result += 'idList=' + ids[i]
        if (i < ids.length - 1) {
            result += '&'
        }
    }
    return '?' + result
}

// 向qt发消息
export const receive = async (sendObj, receiveJSON) => {
    const jsonParam = {
        RequestUrl: REQUEST_URL + receiveJSON.RequestUrl,
        RequestMethod: receiveJSON.RequestMethod,
        Headers: {
            contentType: receiveJSON.contentType
        },
        RequestBody: getQueryParams(receiveJSON.RequestBody, receiveJSON.isStringParam),
        WebInfo: { requestId: receiveJSON.requestId }
    }
    const stringParam = JSON.stringify(jsonParam)
    await sendObj.receiveMessage(stringParam)
}
